package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.result.JWTUtils;
import com.atguigu.commonutils.result.R;
import com.atguigu.commonutils.result.vo.UcenterMemberVo;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.feign.UCenterFeignService;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.servicebase.exception.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @create 2023-05-17 15:20
 */
@RestController
@RequestMapping("eduservice/comment")
public class CommentController {
    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UCenterFeignService uCenterFeignService;

    @GetMapping("{page}/{limit}/{courseId}")
    public R page(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @PathVariable("courseId") Long courseId,HttpServletRequest request) {
        Page<EduComment> pageParam = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByDesc("gmt_create");
        commentService.page(pageParam, wrapper);
        List<EduComment> commentList = pageParam.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());

        String token = request.getHeader("token");
        Map<String, Object> m = JWTUtils.checkToken(token);
        String id = (String) m.get("id");
        map.put("memberId", id);

        return R.ok().data(map);
    }

    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request) {
        if(!StringUtils.hasText(comment.getContent())){
            throw new MyException(20001,"评论不能为空");
        }
        String token = request.getHeader("token");
        Map<String, Object> map = JWTUtils.checkToken(token);
        String id = (String) map.get("id");
        UcenterMemberVo member = uCenterFeignService.getMember(id);
        comment.setMemberId(member.getId());
        comment.setNickname(member.getNickname());
        comment.setAvatar(member.getAvatar());
        commentService.save(comment);


        return R.ok();
    }

    @DeleteMapping("/removeComment")
    public R removeComment(@RequestParam("id")String id) {
        this.commentService.removeById(id);
        return R.ok();
    }
}
